// Copyright (c) 2010, Sarah Brennan, Sales Support Engineering, Salesforce.com Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// Neither the name of the salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Author: Sarah Brennan
// Date: 21-10-2010

// ----------------------------------------------------------------------------------------------------------------------------------



//THIS TRIGGER ADDS AN EVENT TO THE CALENDAR ONCE A NEW OUTLET VISIT IS CREATED OR AN EVENT IS UPDATED.




trigger visitTrigger on Outlet_Visit__c (after insert, after update){

   //create a list that holds new events
    List<Event> events = new List<Event>();
     integer i;
    //actions if record is being inserted 

        if(trigger.isinsert){ 
        
        
       
            //for loop that defines new event
            for (Outlet_Visit__c ov : System.Trigger.new){
                if(ov.planned_duration__c == null || ov.planned_duration__c == 0)
                    i = 60;
                else
                    i = ov.planned_duration__c.intValue();
                
                Event event = new Event(
                    StartDateTime = ov.Planned_time__c , //Changed by CdG
                    EndDateTime = ov.Planned_time__c.addMinutes(i), //changed by CdG
                    Subject = 'Planned visit',
                    IsRecurrence = FALSE,
                    Description = ov.Comment__c,
                    Location = ov.Outlet_Name__c, 
                    Event_ID__c = ov.id,
                    IsAllDayEvent = False,
                    WhatID = ov.id); //Changed by CdG
                    
                    system.debug('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& event:'+event);
                    
              
                
                events.add(event);            
            } 
            
            
            
        
        //adds events     
                        
        if(events.size() > 0) insert events; 
                
        }
        
        //closes if statement and states actions for all other triggers besides insert 
        
   else{        
       
       for (Outlet_Visit__c ov : System.Trigger.new){
            if(ov.planned_duration__c == null || ov.planned_duration__c == 0)
                    i = 60;
                else
                    i = ov.planned_duration__c.intValue();
            
            List<Event> eventList  = [SELECT Event_ID__c, StartDateTime FROM Event WHERE Event_ID__c = : ov.id];
            List<Event> updateList  = new List<Event>();
            
            for (Event event : eventList){
                if(event.StartDateTime != ov.Planned_time__c){
                    event.StartDateTime = ov.Planned_time__c;
                    event.EndDateTime = ov.Planned_time__c.addMinutes(i);
                    event.Subject = 'Planned visit'; 
                    event.Description = ov.Comment__c;
                    event.Location = ov.Outlet_Name__c; 
                    updateList.add(event);
                }
             }  
              
             if(updateList.size() > 0) 
                 update updateList;                
       }
                   
       
       
     }  
       
       
       
       
       
       /* 
        //actions if record is being updated 
            
            //creates a list that holds Event IDs
            
            Set<String> idSetEvent = new Set<String>(); 
            
            //for loop that adds the Event_ID__c
            
            for (Outlet_Visit__c ov : System.Trigger.New) { //for loop that adds the Event id 
                idSetEvent.add(ov.Event__c);
            } 
            
            
            // creates a map to hold the event_ID__c that needs to be updated
            
            Map<String, Event> evMap = new Map<String, Event>(); 
             
            //inserts Event_ID__c, ev into evmap Map
            
            for(Event ev :[SELECT Event_ID__c FROM Event WHERE Event_ID__c in : idSetEvent]){         
                evMap.put(ev.Event_ID__c, ev); 
            } 
            
            for (Outlet_Visit__c ov : System.Trigger.New) { 
            
                  Event event = evMap.get(ov.Event__c);
                  
                  // if no Event_ID__c is found present error message
                    if(event == null){ 
                        event.addError('Event ID is wrong');
                        continue;
                    } 
                    
                    event.StartDateTime = ov.Start_Time__c;
                    event.EndDateTime = ov.End_Time__c;
                    event.Subject = 'New Event Scheduled'; 
                    event.Description = ov.Comment__c;
                    event.Location = ov.Name; 
                    events.add(event);
            } 
            
            // updates event 
            if(events.size() > 0) update events;        
        }*/
}